$(function($) {
	var vm = new Vue({
		el : '#_vue_body',
		data : {
			selects : {},
			query : {
				tradeStatus : "",
				subTradeType : "",
				merchantNo : "",
				startTime : "",
				endTime : "",
				outTradeNo : "",
				tradeNo : "",
				payRequestPiNo : "",
				pageSize : "10",
				pageStart : "1",
				pageTotal : "0",
                piType: ""
			},
			lstFlag : false,
			trades : [],
			trypays : []
		},
		methods : {
            getSelects: function () {
                $.ajax({
                    headers: {
                        Accept: "application/json; charset=utf-8"
                    },
                    type: "GET",
                    url: "/aggre/trade/init",
                    dataType: "json",
                    success: function (msg) {
                        console.log("getSelects: " + JSON.stringify(msg));
                        vm.selects = msg;
                    }
                });
            },
            reset4clean: function () {
                this.getSelects();
                vm.query = {
                    tradeStatus: "",
                    subTradeType: "",
                    merchantNo: "",
                    startTime: "",
                    endTime: "",
                    outTradeNo: "",
                    tradeNo: "",
                    payRequestPiNo: "",
                    pageSize: "10",
                    pageStart: "1",
                    pageTotal: "0",
                    piType: ""
                };
                $('#input_startTime').val('');
                $('#input_endTime').val('');
            },
            lst: function (flag) {
                if(flag == 1 || flag == "1"){
                    vm.query.pageStart = 1;
                }
                vm.query.startTime = $('#input_startTime').val();
                vm.query.endTime = $('#input_endTime').val();
                vm.trades = [];
                console.log("query:" + JSON.stringify(vm.query));
                var startNum = parseInt(vm.query.startTime.replace(/-/g, ''), 10);
                var endNum = parseInt(vm.query.endTime.replace(/-/g, ''), 10);
				if(vm.query.merchantNo==""&&vm.query.tradeNo==""&&vm.query.payRequestPiNo==""&&vm.query.outTradeNo=="") {
					if (startNum > endNum) {
						layer.msg("无条件查询时结束时间必须大于开始时间");
						return false;
					}
					//if (vm.query.startTime == null || vm.query.startTime == "" || vm.query.endTime == null || vm.query.endTime == "") {
					//	layer.msg("查询起始时间不能为空");
					//	return false;
					//}
					//if (endNum - startNum > 3) {
					//	layer.msg("只能查询近期三天内的数据");
					//	return false;
					//}
					//if (endNum - startNum < 1) {
					//	layer.msg("最少要能查一天");
					//	return false;
					//}
				}


                if (vm.lstFlag) {
                    return;
                }
                vm.lstFlag = true;

                $.ajax({
                    headers: {
                        Accept: "application/json; charset=utf-8"
                    },
                    type: "POST",
                    url: "/aggre/trade/lst",
                    data: this.query,
                    dataType: "json",
                    success: function (msg) {
//						 console.log("lst: " + JSON.stringify(msg));
                        var total = Math.floor(msg.pageTotal / vm.query.pageSize);
                        total = msg.pageTotal % vm.query.pageSize > 0 ? total + 1 : total;
                        vm.query.pageTotal = total;
                        if (msg.trades) {
                            var lst = [];
                            $.each(msg.trades, function (i, item) {
                                var trade = item.trade;
                                if (item.trade) {
                                    trade.createdDate = new Date(Number(trade.createdDate)).format("yyyy-MM-dd hh:mm:ss");
                                    lst.push(trade);
                                }
                                if (item.pays) {
                                    $.each(item.pays, function (j, pay) {
                                        pay.ispay = 1;
                                        pay.createdDate = new Date(Number(pay.createdDate)).format("yyyy-MM-dd hh:mm:ss");
                                        lst.push(pay);
                                    })
                                }
                            })
                            vm.trades = lst;
                        } else {
                            layer.msg("没有查到数据，请确认查询条件！");
                        }
                        vm.lstFlag = false;
                    }
                });
                vm.trypays = [];
            },
            queryPay: function () {
                console.log("queryPay: " + JSON.stringify(vm.trypays.length));
                if (vm.trypays.length <= 0) {
                    layer.msg("请选择补单数据");
                    return;
                }
                if (vm.lstFlag) {
                    return;
                }
                vm.lstFlag = true;
                $.each(vm.trypays, function (i, item) {
                    $.ajax({
                        headers: {
                            Accept: "application/json; charset=utf-8"
                        },
                        type: "POST",
                        url: "/aggre/trade/q",
                        data: item,
                        dataType: "json",
                        success: function (msg) {
                            console.log("queryPay-for: [" + item.payRequestPiNo + "], queryPay-return: " + JSON.stringify(msg));
                        }
                    });
                })
                layer.msg("补单提交完毕，请稍候查询");
                vm.lstFlag = false;
                this.lst();
            },
            payNext: function (current, flag) {
                current = Number(current);
                var start = current;
                if (flag === '1') {
                    current = current - 1 < 1 ? 1 : current - 1;
                }
                if (flag === '0') {
                    current = current + 1 > vm.query.pageTotal ? current : current + 1;
                }
                if (start != current) {
                    vm.query.pageStart = current;
                    this.lst();
                } else {
                    layer.msg("没有数据了")
                }
            },
        },
		mounted : function() {
			// this.getSelects();
		}
	})

})

Date.prototype.format = function(fmt) {
	var o = {
		"M+" : this.getMonth() + 1, //月份 
		"d+" : this.getDate(), //日 
		"h+" : this.getHours(), //小时 
		"m+" : this.getMinutes(), //分 
		"s+" : this.getSeconds(), //秒 
		"q+" : Math.floor((this.getMonth() + 3) / 3), //季度 
		"S" : this.getMilliseconds()
	//毫秒 
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}
	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
					: (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}